{% template "admin/header.html" %}
<div style="margin: 15px;" class="fadeInUp animated">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>添加权限</legend>
    </fieldset>
    <blockquote class="layui-elem-quote">
        图标名称为左侧导航栏目的图标样式，具体可查看<a href="https://icomoon.io/app/#/select" target="_blank">premium</a>图标
    </blockquote>
    <form class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <label class="layui-form-label">父级</label>
            <div class="layui-input-4">
                <select name="pid" lay-verify="required" lay-filter="pid" id="auths">
                    <option value="0">默认顶级</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">权限名称</label>
            <div class="layui-input-4">
                <input type="text" name="title" lay-verify="required" placeholder="权限名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">路由</label>
            <div class="layui-input-4">
                <input type="text" name="href" lay-verify="required" placeholder="路由" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">图标名称</label>
            <div class="layui-input-4">
                <input type="text" name="icon" placeholder="图标名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">菜单状态</label>
            <div class="layui-input-block">
                <input type="radio" name="menustatus" lay-filter="menustatus" checked value="1" title="开启">
                <input type="radio" name="menustatus" lay-filter="menustatus" value="0" title="关闭">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">菜单</label>
            <div class="layui-input-block">
                <input type="radio" name="is_menu" lay-filter="is_menu" checked value="1" title="是">
                <input type="radio" name="is_menu" lay-filter="is_menu" value="0" title="否">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-2">
                <input type="text" name="sort" value="50" placeholder="排序编号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="auth">立即提交</button>
                <a href="javascript:window.history.back();" class="layui-btn layui-btn-primary">返回</a>
            </div>
        </div>
    </form>
</div>
{% template "admin/footer.html" %}
<script>
    var pid = getUrlParam("pid");
    layui.use(['form', 'layer'], function () {
        var form = layui.form,layer = layui.layer,$ = layui.jquery;

        var menu_cache = null;
        $.post('/admin/menu/query',function (data) {
            menu_cache = [];
            for(var i=0;i<data.Data.length;i++){
                menu_cache.push(upperJSONKey(data.Data[i]))
            }
            getTrList(menu_cache);
        });

        /**
         * $.each(data.data,function (k,v) {
    $('#auths').append('<option value="'+v.id+'" '+(pid==v.id?'selected':'')+'>'+v.ltitle+'</option>');
    });
         * @param data
         */
        function hanMenu(data,pid,level){
            var arr = [];
            var tem = null;
            for (var i=0; i<data.length; i++){
                if(data[i].pid == pid){
                    tem = data[i];
                    tem['lvl'] = level + 1;
                    tem['lefthtml'] = "|— ".repeat(level);
                    tem['ltitle'] = tem['lefthtml'] + tem.title;
                    arr.push(tem);
                    arr = arr.concat(hanMenu(data,data[i].id,tem['lvl']));
                }
            }
            return arr;
        }

        function getTrList(data){
            var arr = [];
            arr = hanMenu(data,0,0);
            for (var i=0;i<arr.length;i++){
                $('#auths').append('<option value="'+arr[i].id+'" '+(pid==arr[i].id?'selected':'')+'>'+arr[i].ltitle+'</option>');
            }
            form.render();
        }


        form.on('submit(auth)', function (data) {
            // 提交到方法 默认为本身
            $.post("/admin/menu/add",data.field,function(res){
                if(res.Code > 0){
                    layer.msg(res.Msg,{time:1000,icon:1},function(){
                        //window.history.back();
                    });
                }else{
                    layer.msg(res.Msg,{time:1000,icon:2});
                }
            });
        });
    });
</script>
</body>
</html>